package com.ruoyi.service.jpjs.project.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.mapper.SysParameterMapper;
import com.ruoyi.mapper.jpjs.building.BuildingGGMapper;
import com.ruoyi.mapper.jpjs.param.ParamMapper;
import com.ruoyi.mapper.jpjs.project.ProjectGGMapper;
import com.ruoyi.service.jpjs.project.ProjectServiceGG;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.mapper.SysUserProjectMapper;
import com.ruoyi.util.ExcelUtils;
import com.ruoyi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProjectServiceImplGG implements ProjectServiceGG {

    @Autowired
    private ProjectGGMapper projectMapper;

    @Autowired
    private ParamMapper paramMapper;

    @Autowired
    private SysUserProjectMapper sysUserProjectMapper;

    @Autowired
    private SysParameterMapper sysParameterMapper;

    @Autowired
    private BuildingGGMapper buildingMapper;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    @Override
    public List<Map<String, Object>> findProjectList(Map<String, Object> map) {
        return projectMapper.findProjectList(map);
    }

    @Override
    public int savePorject(Map<String, Object> map) {
        int count = 0;
        if (map.get("id") != null && !map.get("id").equals("")) {
            count = projectMapper.updatePorject(map);
            if (count > 0) {
                count = projectMapper.updateProInformation(map);
                buildingMapper.deleteBuilding(map.get("id").toString());
                addProBuilding(map);
            }
        } else {
            String proId = StringUtil.getUUID();
            String informationId = StringUtil.getUUID();
            map.put("id", proId);
            count = projectMapper.addPorject(map);
            addProBuilding(map);
            if (count > 0) {
                map.put("id", informationId);
                map.put("proId", proId);
                count = projectMapper.addProInformation(map);
            }
        }
        return count;
    }

    @Override
    public Map<String, Object> findProject(String id) {
        Map<String, Object> map = projectMapper.findProject(id);
        boolean netwok = (int) map.get("network") == 1 ? true : false;
        boolean erp = (int) map.get("erp") == 1 ? true : false;
        boolean attendance = (int) map.get("attendance") == 1 ? true : false;
        boolean monitor = (int) map.get("monitor") == 1 ? true : false;
        boolean devops = (int) map.get("devops") == 1 ? true : false;
        map.put("netwok", netwok);
        map.put("erp", erp);
        map.put("attendance", attendance);
        map.put("monitor", monitor);
        map.put("devops", devops);
        if (map.get("startTime") == null) {
            map.put("startTime", "");
        }
        if (map.get("endTime") == null) {
            map.put("endTime", "");
        }

        return map;
    }

    @Override
    public int removePorject(String ids) {
        int count = 0;
        String[] arr = ids.split(",");
        for (String id : arr) {
            count = projectMapper.removePorject(id);
            if (count > 0) {
                buildingMapper.deleteBuilding(id);
                count = projectMapper.removeProInformation(id);
            } else {
                return count;
            }
        }
        return count;
    }

    @Override
    public List<Map<String, Object>> getProView() {
        return projectMapper.getProView();
    }

    @Override
    public AjaxResult export(Map<String, Object> map) {
        List<Map<String, Object>> list = projectMapper.findProjectList(map);
        List<Map<String, String>> colNameList = paramMapper.getColumns("fi_project_view");
        List<String> colList = new ArrayList<String>();
        for (Map<String, String> colMap : colNameList) {
            colList.add(colMap.get("colName"));
        }
        return ExcelUtils.exportData(colList, list, "项目信息");
    }

    @Override
    public List<Map<String, Object>> getProjectName() {
        return projectMapper.getProjectName();
    }

    @Override
    public List<Map<String, Object>> getProjectByUserId(Long userId) {
        List<Map<String, Object>> listPro = projectMapper.getProjectName();
        List<String> listProId = sysUserProjectMapper.getProjectByUserId(userId);
        for (Map<String, Object> map : listPro) {
            if (listProId.size() == 0) {
                map.put("flag", false);
            }
            for (String proId : listProId) {
                boolean flag = map.get("id").toString().equals(proId);
                if (flag) {
                    map.put("flag", flag);
                    break;
                }
                map.put("flag", flag);
            }
        }
        return listPro;
    }

    @Override
    public List<Map<String, Object>> getBuildingByName(String proId) {
//        List<Map<String, Object>> listParameter = sysParameterMapper.findNameByType("楼号");
        List<Map<String, Object>> listParameter = sysDictDataMapper.selectDictDataByTypeList("build_number");


        List<String> listBuildingName = buildingMapper.getBuildingByName(proId);
        for (Map<String, Object> map : listParameter) {
            if (listBuildingName.size() == 0) {
                map.put("flag", false);
            }
            for (String buildingName : listBuildingName) {
                boolean flag = map.get("dict_code").toString().equals(buildingName);
                if (flag) {
                    map.put("flag", flag);
                    break;
                }
                map.put("flag", flag);
            }
        }
        return listParameter;
    }

    @Override
    public List<Ztree> getProjectTree() {
        return initZtree(projectMapper.getProjectTree(ShiroUtils.getSysUser().getUserId()));
    }

    @Override
    public String findCompId(String proId) {
        return projectMapper.findCompId(proId);
    }

    @Override
    public String identifier() {
        return projectMapper.identifier();
    }

    @Override
    public List<Map<String, Object>> findBuilding(Map<String, Object> map) {
        return projectMapper.findBuilding(map);
    }

    public List<Ztree> initZtree(List<Map<String, Object>> projectList) {
        List<Ztree> ztrees = new ArrayList();
        for (Map<String, Object> map : projectList) {
            Ztree ztree = new Ztree();
            ztree.setId((String) map.get("id"));
            ztree.setpId((String) map.get("parentId"));
            ztree.setName((String) map.get("name"));
            ztree.setParentName((String) map.get("parentName"));
            ztree.setTitle((String) map.get("name"));
            ztree.setGrade((String) map.get("grade"));
            ztrees.add(ztree);
        }
        return ztrees;
    }

    //添加用户和项目之间关系
    public void addProBuilding(Map<String, Object> map) {
        String[] buildingIds = map.get("buildingIds").toString().split(",");
        if (StringUtils.isNotEmpty(buildingIds[0])) {
            // 新增用户与角色管理
            List<Map<String, Object>> list = new ArrayList();
            for (String buildingId : buildingIds) {
                Map<String, Object> builMap = new HashMap();
                builMap.put("proId", map.get("id"));
                builMap.put("buildingName", buildingId);
                list.add(builMap);
            }
            if (list.size() > 0) {
                buildingMapper.addBuilding(list);
            }
        }
    }
}
